package com.example.feifei.utils;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ScrollView;
import android.widget.Toast;

/**
 * 滑动Scrollview，检测滑动距离
 * Created by feifei on 2017/7/6.
 */

public class MyScrollView extends ScrollView{

    private OnScrollViewListener scrollViewListener;

    private float downX;//按下时的X坐标
    private float downY;//按下时的Y坐标

    //为外界设置监听
    public void setScrollViewListener(OnScrollViewListener scrollViewListener){
        this.scrollViewListener = scrollViewListener;
    }

    public MyScrollView(Context context) {
        super(context);
    }

    public MyScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public interface OnScrollViewListener{
        void onScrollView(int scrollY);
    }

    @Override
    public void computeScroll() {
        super.computeScroll();
        if(scrollViewListener != null){
            scrollViewListener.onScrollView(getScrollY());
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        //在触发时的起始坐标
        float x = ev.getX();
        float y = ev.getY();
        switch (ev.getAction()){
            case MotionEvent.ACTION_DOWN:
//                Log.e("tag","按下事件");
                downX = x;
                downY = y;
                break;
            case MotionEvent.ACTION_MOVE:
//                Log.e("tag","滑动事件");
                break;
            case MotionEvent.ACTION_UP:
//                Log.e("tag","抬起事件");
                //获取距离差
                float dx = x - downX;
                float dy = y - downY;
                //防止按下的时候也判断
                if(Math.abs(dx)>8&&Math.abs(dy)>8){
                    //通过距离来判断滑动方向
                    int orientation = getOrientation(dx,dy);
                    switch (orientation){
                        case 'r':
                            Toast.makeText(getContext(),"向右滑动",Toast.LENGTH_SHORT).show();
                            break;
                        case 'l':
                            Toast.makeText(getContext(),"向左滑动",Toast.LENGTH_SHORT).show();
                            break;
                        case 't':
                            Toast.makeText(getContext(),"向上滑动",Toast.LENGTH_SHORT).show();
                            break;
                        case 'b':
                            Toast.makeText(getContext(),"向下滑动",Toast.LENGTH_SHORT).show();
                            break;
                    }

                }

                break;
        }

        return super.onTouchEvent(ev);
    }
    //根据距离差判断滑动方向
    private int getOrientation(float dx,float dy){
//        Log.e("tag","X轴的距离差======"+dx);
//        Log.e("tag","Y轴的距离差======"+dy);
        if(Math.abs(dx)>Math.abs(dy)){
               return dx>0?'r':'l';
        }else{
            return dy>0?'b':'t';
        }

    }


}
